package com.fuyu.system.controller;


import com.fuyu.common.model.R;
import com.fuyu.common.web.controller.BaseController;
import com.fuyu.common.web.validation.group.AddGroup;
import com.fuyu.common.web.validation.group.EditGroup;
import com.fuyu.system.models.form.SysUserForm;
import com.fuyu.system.models.search.SysUserSearch;
import com.fuyu.system.models.vo.user.SysUserBaseInfoVO;
import com.fuyu.system.models.vo.user.SysUserListVO;
import com.fuyu.system.service.SysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@Tag(name = "用户管理")
@RequestMapping("/sys/user")
@RestController
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "获取用户基本信息")
    @GetMapping("baseInfo")
    public R<SysUserBaseInfoVO> getUserBaseInfo() {
        String userId = getUserId();
        return R.success(sysUserService.getUserBaseInfo(userId));
    }

    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public R<Boolean> addUser(@RequestBody @Validated({AddGroup.class}) SysUserForm params) {
        return R.success(sysUserService.addUser(params.toEntity(params)));
    }

    @Operation(summary = "编辑用户")
    @PostMapping("/edit")
    public R<Boolean> editUser(@RequestBody @Validated({EditGroup.class}) SysUserForm params) {
        return R.success(sysUserService.editUser(params.toEntity(params)));
    }


    /**
     * 获取用户列表
     */
    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    public R<PageInfo<SysUserListVO>> getUserList(SysUserSearch params) {
        return R.success(sysUserService.getList(params));
    }
}
